package com.groupbuying.cheetah.order.orderForApp;

import com.groupbuying.cheetah.common.PageUtils;
import com.groupbuying.cheetah.common.Query;
import com.groupbuying.cheetah.order.orderCounterFlow.dao.OrderCounterFlowDao;
import com.groupbuying.cheetah.order.orderCounterFlow.domain.OrderCounterFlowDo;
import com.groupbuying.cheetah.utils.apiresult.ApiResult;
import com.groupbuying.cheetah.utils.apiresult.AppApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Vincentxin
 * @Date 2020/1/10
 */
@RestController
@RequestMapping("/mobileApp")
public class OrderForAppController {
    @Autowired
    private OrderCounterFlowDao orderCounterFlowDao;

    /**
     * 我的订单列表
     */
    @GetMapping("/orderListForApp")
    public ApiResult orderCounterFlowList(@RequestParam Map<String, Object> params){
        String customerUuid = (String) params.get("customerId");
        String mobile = (String) params.get("mobile");
        String weChatUnionId = (String) params.get("weChatUnionId");
        String aliUserId = (String) params.get("aliUserId");
        //TODO 通过够样用户ID和手机号微信unionId、openId；支付宝 用户id 查询对应付优店账号;
        Map<String,String> param = new HashMap<>();
        param.put("customerUuid",customerUuid);
        param.put("mobile",mobile);
        param.put("weChatUnionId",weChatUnionId);
        param.put("aliUserId",aliUserId);
        List<String> customerIds = orderCounterFlowDao.getCustomerIdForAppSearch(param);
//
        params.put("customerIds",customerIds);
        Query query = new Query(params);
        //优惠买单、够样优店列表
        List<OrderCounterFlowDo> orderCounterFlowList = orderCounterFlowDao.orderSearchForApp(query);
        for (OrderCounterFlowDo orderCounterFlowDo : orderCounterFlowList) {
            //根据支付类型区分支付金额
            if ("微信支付".equals(orderCounterFlowDo.getPayType())||"支付宝支付".equals(orderCounterFlowDo.getPayType())) {
                orderCounterFlowDo.setPayAmount(orderCounterFlowDo.getPayMoney());
            } else if ("积分支付".equals(orderCounterFlowDo.getPayType())) {
                orderCounterFlowDo.setPayAmount(orderCounterFlowDo.getPayCur()+"积分");
            } else if ("余额支付".equals(orderCounterFlowDo.getPayType())) {
                orderCounterFlowDo.setPayAmount(orderCounterFlowDo.getPayBalance());
            }
            //根据订单编号5-7位区分订单类型
            String subOder = orderCounterFlowDo.getOrderId().substring(4,7);
            if ("000".equals(subOder)) {
                orderCounterFlowDo.setOrderType("够样优店");
            } else if ("001".equals(subOder)) {
                orderCounterFlowDo.setOrderType("优惠买单");
            } else if ("002".equals(subOder)) {
                orderCounterFlowDo.setOrderType("积分换购商品");
                //积分换购商品名店名称统一为“积分自营换购”
                orderCounterFlowDo.setCounterName("积分自营换购");
            }
        }
        int total = orderCounterFlowDao.countSearchForApp(query);
        PageUtils pageUtils = new PageUtils(orderCounterFlowList, total);
        return AppApiResult.success(pageUtils);
    }
}
